package com.it;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.it.bean.R;
import com.it.utils.ConstantPropertiesUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("upload")
@RestController
@CrossOrigin
public class UploadWangController {


    @PostMapping("wang")
    public JSON upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();// 获取上传的文件名
        int i = originalFilename.lastIndexOf("."); // 查看最后一个出现.的位置
        String houzui = originalFilename.substring(i);  // 截取 这个索引开始也就是.开始的后缀
        UUID uuid = UUID.randomUUID();
        String url = ConstantPropertiesUtils.ym
                + "://" + ConstantPropertiesUtils.ip + ":" + ConstantPropertiesUtils.port +
                "/upload02/" + uuid + houzui;
        String rootFilePath = System.getProperty("user.dir") + "\\api\\src\\main\\resources\\upload02\\" + uuid + houzui; // 上传到这
        String property = System.getProperty("user.dir");
        File file1 = FileUtil.writeBytes(file.getBytes(), rootFilePath);
        JSONObject json = new JSONObject();
        json.set("errno", 0);
        JSONArray arr = new JSONArray();
        JSONObject data = new JSONObject();
        arr.add(data);
        data.set("url", url);
        json.set("data", arr);
        return json;
//        JSONObject json = new JSONObject();
//        json.set("errno", 0);
//        JSONArray arr = new JSONArray();
//        JSONObject data = new JSONObject();
//        arr.add(data);
//        data.set("url", url);
//        json.set("data", arr);
//        return json;

    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("测试", "11");
        System.out.println("jsonObject = " + jsonObject);
    }
}
